package com.white.commonlib.widget;

import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.StyleRes;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.white.commonlib.R;
import com.white.commonlib.utils.DrawableUtils;

import butterknife.BindView;

/**
 * Created by yutf on 2017/5/17 0017.
 */

public class BaseDialog extends Dialog
{
    private TextView title;
    private TextView subTitle;
    private TextView msg;
    private TextView btnNegative;
    private TextView btnPositive;
    private View dv;
    private View dh;
    private LinearLayout btnLayout;

    private ImageView ivExit;

    private ViewGroup parent;
    private LinearLayout root;
    private FrameLayout viewContainer;

    public FrameLayout getViewContainer()
    {
        return viewContainer;
    }

    public View getMainFrame()
    {
        return root;
    }

    public TextView getTitle()
    {
        return title;
    }

    public TextView getSubTitle()
    {
        return subTitle;
    }

    public TextView getMsg()
    {
        return msg;
    }

    public TextView getBtnNegative()
    {
        return btnNegative;
    }

    public TextView getBtnPositive()
    {
        return btnPositive;
    }

    public View getDv()
    {
        return dv;
    }

    public View getDh()
    {
        return dh;
    }

    public LinearLayout getBtnLayout()
    {
        return btnLayout;
    }

    public ImageView getIvExit()
    {
        return ivExit;
    }

    public BaseDialog(@NonNull Context context)
    {
        this(context,R.style.ShareDialogStyle);
    }

    public BaseDialog(@NonNull Context context, @StyleRes int themeResId)
    {
        super(context, themeResId);
        parent = (ViewGroup) LayoutInflater.from(context).inflate(R.layout.dialog_base, null);
        root = (LinearLayout) parent.findViewById(R.id.root);
        viewContainer = (FrameLayout) parent.findViewById(R.id.view_container);
        title = (TextView) parent.findViewById(R.id.title);
        subTitle = (TextView) parent.findViewById(R.id.sub_title);
        ivExit = (ImageView) parent.findViewById(R.id.exit);
        msg = (TextView) parent.findViewById(R.id.msg);
        btnPositive = (TextView) parent.findViewById(R.id.btn_positive);
        btnNegative = (TextView) parent.findViewById(R.id.btn_negative);
        dv = parent.findViewById(R.id.dv);
        dh = parent.findViewById(R.id.dh);
        btnLayout = (LinearLayout) parent.findViewById(R.id.btn_layout);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(parent);
        buildSize(8);
    }

    private void buildSize(int ration)
    {
        Window window = getWindow();
        WindowManager.LayoutParams p = window.getAttributes();
        DisplayMetrics m = getContext().getResources().getDisplayMetrics();
        int width = m.widthPixels;
        p.width = (int) (width / (10.0 / ration));
        p.height = WindowManager.LayoutParams.WRAP_CONTENT;
        //此处因为是重新设置了params，所以在xml中设置的动画已经无效了，要再设置一次
        window.setWindowAnimations(R.style.BaseDialogAnim);
        window.setAttributes(p);
    }


    public static class Builder
    {
        private BaseParams p;
        private BaseDialog dialog;
        private boolean isCreated;
        private @StyleRes int theme;

        public Builder(@NonNull Context context)
        {
            this(context, 0);
        }

        public Builder(@NonNull Context context, @StyleRes int themeResId)
        {
            p = new BaseParams(context);
            if(themeResId != 0)
            {
                theme = themeResId;
            }
        }

        /***
         * 如果对话框中间只是要显示一条文本消息，则可以直接把文本传过来
         * @param message
         * @return
         */
        public Builder setMessage(String message)
        {
            if(TextUtils.isEmpty(message))
            {
                p.message = "";
            }else
            {
                p.message = message;
            }
            return this;
        }

        /***
         * 确定按钮点击监听
         * @param positiveText  确定的文本提示，如：(ok 、 确定)
         * @param listener
         * @return
         */
        public Builder setPositiveButton(String positiveText, OnClickListener listener)
        {
            p.positiveText = positiveText;
            p.positiveListener = listener;
            return this;
        }

        public Builder setOnDismissListener(OnDismissListener d)
        {
            p.dismissListener = d;
            return this;
        }

        /***
         * 确定按钮点击监听
         * @param negativeText  取消的文本提示，如：(ok 、 确定)
         * @param listener
         * @return
         */
        public Builder setNegativeButton(String negativeText, OnClickListener listener)
        {
            p.negativeText = negativeText;
            p.negativeListener = listener;
            return this;
        }

        public Builder setExitListener(View.OnClickListener l)
        {
            p.exitListener = l;
            return this;
        }

        public BaseDialog create()
        {
            if(theme == 0)
            {
                dialog = new BaseDialog(p.context, R.style.DialogNoDimenPure);
            }else
            {
                dialog = new BaseDialog(p.context, theme);
            }
            p.apply(dialog);
            isCreated = true;
            return dialog;
        }

        public Builder setTitle(String title)
        {
            p.title = title;
            return this;
        }

        public Builder setSubTitle(String subTitle)
        {
            p.subTitle = subTitle;
            return this;
        }

        public Builder setCancelable(boolean cancelable)
        {
            p.cancelable = cancelable;
            return this;
        }

        public Builder setTitleColor(int c)
        {
            p.titleColor = c;
            return this;
        }

        public Builder setSubTitleColor(int c)
        {
            p.subTitleColor = c;
            return this;
        }

        public Builder setMsgColor(int c)
        {
            p.msgColor = c;
            return this;
        }

        public Builder setBgColor(int c)
        {
            p.bgColor = c;
            return this;
        }

        public Builder setBtnTextColor(int c)
        {
            p.btnTextColor = c;
            return this;
        }

        public Builder setView(View v)
        {
            p.contentView = v;
            return this;
        }

        //显示对话框
        public BaseDialog show()
        {
            if (!isCreated)
            {
                dialog = create();
                dialog.show();
            } else
            {
                if (dialog.isShowing())
                {
                    dialog.dismiss();
                }
                dialog.show();
            }
            return dialog;
        }
    }

    protected static class BaseParams
    {
        public OnClickListener negativeListener, positiveListener;
        public View.OnClickListener exitListener;
        public OnDismissListener dismissListener;
        public boolean cancelable = true;
        public boolean showExit = false;
        public String negativeText, positiveText;
        //默认会带一个textview显示message
        public String message;
        public String title;
        public String subTitle;
        public Context context;
        public View contentView;
        public int titleColor;
        public int subTitleColor;
        public int msgColor;
        public int bgColor;
        public int btnTextColor = 0xFF2c97fc;

        public BaseParams(Context context)
        {
            this.context = context;
        }

        public void apply(final BaseDialog dialog)
        {

            if (bgColor != 0)
            {
                float dp = dialog.getContext().getResources().getDisplayMetrics().density;
                Drawable d = DrawableUtils.createShape(dp * 10, bgColor);
                dialog.getMainFrame().setBackgroundDrawable(d);
            }

            //取消
            if (!TextUtils.isEmpty(negativeText))
            {
                dialog.getBtnNegative().setText(negativeText);
                dialog.getBtnNegative().setTextColor(btnTextColor);
                dialog.getBtnNegative().setOnClickListener(new View.OnClickListener()
                {
                    @Override
                    public void onClick(View v)
                    {
                        dialog.dismiss();
                        if (negativeListener != null)
                        {
                            negativeListener.onClick(dialog, 0);
                        }
                    }
                });
            }

            //确定
            if (!TextUtils.isEmpty(positiveText))
            {
                dialog.getBtnPositive().setText(positiveText);
                dialog.getBtnPositive().setTextColor(btnTextColor);
                dialog.getBtnPositive().setOnClickListener(new View.OnClickListener()
                {

                    @Override
                    public void onClick(View v)
                    {
                        dialog.dismiss();
                        if (positiveListener != null)
                        {
                            positiveListener.onClick(dialog, 1);
                        }
                    }
                });
            }

            if (exitListener != null)
            {
                dialog.getIvExit().setVisibility(View.VISIBLE);
                dialog.getIvExit().setOnClickListener(new View.OnClickListener()
                {
                    @Override
                    public void onClick(View v)
                    {
                        dialog.dismiss();
                        exitListener.onClick(v);
                    }
                });
            }

            if (!TextUtils.isEmpty(negativeText) && TextUtils.isEmpty(positiveText))
            {
                dialog.getDv().setVisibility(View.GONE);
                dialog.getBtnPositive().setVisibility(View.GONE);
                dialog.getBtnNegative().setBackgroundDrawable(context.getResources().getDrawable(R.drawable.bs_dialog_onebtn_only));
            } else if (TextUtils.isEmpty(negativeText) && !TextUtils.isEmpty(positiveText))
            {
                dialog.getDv().setVisibility(View.GONE);
                dialog.getBtnNegative().setVisibility(View.GONE);
                dialog.getBtnPositive().setBackgroundDrawable(context.getResources().getDrawable(R.drawable.bs_dialog_onebtn_only));
            } else if (TextUtils.isEmpty(negativeText))
            {
                dialog.getBtnLayout().setVisibility(View.GONE);
                //分割线也隐藏
                dialog.getDh().setVisibility(View.GONE);
            }

            if (dismissListener != null)
                dialog.setOnDismissListener(dismissListener);

//            Window wd = dialog.getWindow();
//            if(wd != null)
//            {
//                if(!cancelable)
//                {
//                    wd.setFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
//                        WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);
//                }
//            }
            dialog.setCanceledOnTouchOutside(cancelable);
            if (!cancelable)
            {
                dialog.setCancelable(cancelable);
            }

            if (title != null)
            {
                dialog.getTitle().setVisibility(View.VISIBLE);
                dialog.getTitle().setText(title);
            } else
            {
                dialog.getTitle().setVisibility(View.GONE);
            }

            if (subTitle != null)
            {
                dialog.getSubTitle().setVisibility(View.VISIBLE);
                dialog.getSubTitle().setText(subTitle);
            } else
            {
                dialog.getSubTitle().setVisibility(View.GONE);
            }

            if (message != null)
            {
                dialog.getMsg().setText(message);
            } else
            {
                dialog.getMsg().setVisibility(View.GONE);
            }

            if (contentView != null)
            {
                dialog.getViewContainer().addView(contentView,
                        new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                                ViewGroup.LayoutParams.WRAP_CONTENT));
            }

            if (titleColor != 0)
            {
                dialog.getTitle().setTextColor(titleColor);
            }
            if (subTitleColor != 0)
            {
                dialog.getSubTitle().setTextColor(subTitleColor);
            }
            if (msgColor != 0)
            {
                dialog.getMsg().setTextColor(msgColor);
            }
        }
    }
}
